
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：汇率表                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-01-29 17:12:18                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： ExchangeRateService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Models;
using BestTrader.Cms.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BestTrader.Cms.Services
{
    public class ExchangeRateService: IExchangeRateService
    {
        private readonly IExchangeRateRepository _repository;
        private readonly IManagerRepository _managerRepository;
        private readonly IMapper _mapper;

        public ExchangeRateService(IExchangeRateRepository repository, IManagerRepository managerRepository, IMapper mapper)
        {
            _repository = repository;
            _managerRepository = managerRepository;
            _mapper = mapper;
        }
        
        public BaseResult Setting(ExchangeRateSettingModel item, string UserId)
        {
            var result = new BaseResult();
            var userId = UserId.ToInt32() ?? 0;
            var now = DateTime.Now;

            var entity = _repository.Get(item.Id);
            if (entity != null)
            {
                entity.ProjectPrice = item.ProjectPrice;
                entity.ProjectTime = now;
                entity.ModifyManagerId = userId;
                entity.ModifyTime = now;
                if (_repository.Update(entity) > 0)
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
            }
            return result;
        }

        public BaseResult AddOrModify(ExchangeRateAddOrModifyModel item, string UserId)
        {
            var result = new BaseResult();
            ExchangeRate entity;
            var userId = UserId.ToInt32() ?? 0;
            var now = DateTime.Now;
            if (item.Id == 0)
            {
               var checkExist = _repository.GetList($"WHERE IsDelete=0 AND ProjectManagerId ={item.ProjectManagerId} AND ProjectCode= '{item.ProjectCode}'").FirstOrDefault();
                if (checkExist == null)
                {
                    entity = _mapper.Map<ExchangeRate>(item);
                    entity.ProjectTime = now;
                    entity.AddManagerId = userId;
                    entity.IsDefault = false;
                    entity.IsDelete = false;
                    entity.AddTime = now;
                    if (_repository.Insert(entity) > 0)
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }
                }
                else {

                    result.ResultCode = ResultCodeAddMsgKeys.ExchangeRateFailNoDataCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.ExchangeRateFailPepeatDataMsg;
                }
            }
            else
            {
                //TODO Modify
                entity = _repository.Get(item.Id);
                if (entity != null)
                {
                    //_mapper.Map(item, entity);
                    entity.ProjectPrice = item.ProjectPrice;
                    entity.ProjectManagerId = item.ProjectManagerId;
                    entity.ProjectTime = now;
                    entity.ModifyManagerId = userId;
                    entity.ModifyTime = now;
                    if (_repository.Update(entity) > 0)
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
                }
            }
            return result;
        }

        public TableDataModel LoadData(ExchangeRateRequestModel model, string UserId, string RoleId)
        {
            string conditions = "where IsDelete=0 ";//未删除的
            if (!model.Key.IsNullOrWhiteSpace())
            {
                //conditions += $"and (ProjectName like '%{model.Key}%' or ProjectCode like '%{model.Key}%')";
                conditions += $"and ProjectManagerId in(select Id from Manager where RealName like '%{model.Key}%' or UserName like '%{model.Key}%') ";
            }
            var list = _repository.GetListPaged(model.Page, model.Limit, conditions, "Id desc").ToList();
            var viewList = new List<ExchangeRateListModel>();
            list.ForEach(x => {
                var item = _mapper.Map<ExchangeRateListModel>(x);
                if (x.ProjectManagerId.HasValue)
                    item.ProjectManagerName = _managerRepository.GetUserNameAndRealNameById(x.ProjectManagerId.Value);
                viewList.Add(item);
            });
            return new TableDataModel
            {
                count = _repository.RecordCount(conditions),
                data = viewList,
            };
        }

        public BaseResult DeleteIds(int[] Ids)
        {
            var result = new BaseResult();
            if (Ids.Count() == 0)
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonModelStateInvalidMsg;
            }
            else
            {
                var count = _repository.DeleteLogical(Ids);
                if (count > 0)
                {
                    //成功
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    //失败
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }
            }
            return result;
        }

    }
}